$(document).ready(function () {
    $.ajax({
        type: "GET",
        url: baseURL + "/api/roleGroup/listAll",
        typeData: "json",
        success: function (result) {
            if (result.flag == true) {
                $.each(result.results, function (i, n) {
                    var rgid = result.results[i].id;
                    var rgName = result.results[i].groupName;
                    $("#role_status").append("<option value='" + rgid + "'>" + rgName + "</option>");
                })
                $('#role_status').val(_roleGroupId);
                $('#role_status').next().find('input').val($('#role_status').find("option[value='" + _roleGroupId + "']").text());
                $("#role_code").attr("readOnly", true);
            } else layer.msg('数据错误！');
        },
        error: function (error) {
            layer.msg(error.message);
        }
    })
    $("#form-edit").Validform({
        tiptype: 2,
        showAllError: true,
        callback: function (form) {
            layer.confirm('您确定要提交表单吗？', function () {
                submit();
            })
            return false;
        }

    });
    $("#role_code").attr("readOnly", true);
    if(isManagerPlat=='true'){
		$('#roleGroup').show();
	}else $('#roleGroup').hide();
});

submit = function () {
    var data = {};
    data.id = $('#role_id').val();
    data.name = $('#role_name').val();
    data.code = $('#role_code').val();
    data.descn = $('#role_descn').val();
    if(isManagerPlat=='true'){
    	data.roleGroupId = $('#role_status').val();
	    if (data.roleGroupId == null || data.roleGroupId == "") {
	        layer.msg("角色组不能为空！")
	        return false;
	    }
	}
    $.ajax({
        type: "post",
        url: baseURL + '/api/role/saveOrUpdateRole',
        dataType: "json",
        contentType: "application/json",
        data: JSON.stringify(data),
        success: function (result) {
            if (!result.flag) {
                layer.msg("修改失败！原因：" + result.message);
                return;
            }
            parent.location.reload();
            // var layer = $('.xubox_layer0');
            // layer.close(i);
        }
    });
}
